package gwtappcontainer.testhelpers;

import gwtappcontainer.server.apis.admin.AdminAPI;
import gwtappcontainer.server.apis.admin.Roles.Role;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;

public class APITestHelper {
	
	private final LocalServiceTestHelper dataStoreTestHelper =
	        new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
	        .setDefaultHighRepJobPolicyUnappliedJobPercentage(100));
	    
    private final LocalServiceTestHelper userServiceTestHelper =
	        new LocalServiceTestHelper(new LocalUserServiceTestConfig())
	            .setEnvIsAdmin(true).setEnvIsLoggedIn(true)
	            .setEnvAuthDomain("example.com")
	            .setEnvEmail("test@example.com");
    
    private final String ROLE_DEVELOPER = Role.DEVELOPER.toString();
    private final String ROLE_PORTAL_ADMIN = Role.PORTAL_ADMIN.toString();
    private final String ROLE_PORTAL_USER = Role.PORTAL_USER.toString();
    private final String ROLE_PORTAL_READONLY = Role.PORTAL_READONLY.toString();
    
    private final String EMAIL_DEVELOPER = Role.DEVELOPER.toString() + "@example.com";
    private final String EMAIL_PORTAL_ADMIN = Role.PORTAL_ADMIN.toString() + "@example.com";
    private final String EMAIL_PORTAL_USER = Role.PORTAL_USER.toString() + "@example.com";
    private final String EMAIL_PORTAL_READONLY = Role.PORTAL_READONLY.toString() + "@example.com";    
    
    public User loginAsDeveloper() {
    	userServiceTestHelper.setEnvIsLoggedIn(true)
    		.setEnvEmail(EMAIL_DEVELOPER);
    	
    	return UserServiceFactory.getUserService().getCurrentUser();
    }
    
    public User setLoggedInUserAsAppengineAdmin(boolean isAdmin) {
    	userServiceTestHelper.setEnvIsAdmin(true);
    	
    	return UserServiceFactory.getUserService().getCurrentUser();
    }
            
    public User loginAsPortalAdmin() {
    	userServiceTestHelper.setEnvIsLoggedIn(true)
    		.setEnvEmail(EMAIL_PORTAL_ADMIN);
    	
    	return UserServiceFactory.getUserService().getCurrentUser();
    }
    
    public User loginAsPortalUser() {
    	userServiceTestHelper.setEnvIsLoggedIn(true)
    		.setEnvEmail(EMAIL_PORTAL_USER);
    	
    	return UserServiceFactory.getUserService().getCurrentUser();
    }
    
    public User loginAsPortalReadOnly() {
    	userServiceTestHelper.setEnvIsLoggedIn(true)
    		.setEnvEmail(EMAIL_PORTAL_READONLY);
    	
    	return UserServiceFactory.getUserService().getCurrentUser();
    }
    
    public User loginAs(String email) {
    	userServiceTestHelper.setEnvIsLoggedIn(true).setEnvEmail(email);
    	
    	return UserServiceFactory.getUserService().getCurrentUser();
    }
    
    public void logout() {
    	userServiceTestHelper.setEnvIsLoggedIn(false);
    }
        
	public void setUp() {
		
		dataStoreTestHelper.setUp();
		userServiceTestHelper.setUp();
		
		MockGateKeeper mockGateKeeper = new MockGateKeeper();
		mockGateKeeper.allowRoleToUser("test@example.com", Role.DEVELOPER);
		mockGateKeeper.allowRoleToUser("test@example.com", Role.PORTAL_ADMIN);
		
		AdminAPI api = new AdminAPI();
		api.setGateKeeper(mockGateKeeper);
		
		User user = UserServiceFactory.getUserService().getCurrentUser();
		
		//add roles
		api.addRole(ROLE_DEVELOPER, user);
		api.addRole(ROLE_PORTAL_ADMIN, user);
		api.addRole(ROLE_PORTAL_USER.toString(), user);
		api.addRole(ROLE_PORTAL_READONLY.toString(), user);
		
		//add users
		api.addUser(EMAIL_DEVELOPER, user);
		api.addUser(EMAIL_PORTAL_ADMIN, user);
		api.addUser(EMAIL_PORTAL_READONLY, user);
		api.addUser(EMAIL_PORTAL_USER, user);
		
		//assign roles
		api.assignRoleToUser(EMAIL_DEVELOPER, ROLE_DEVELOPER, user); 				
		api.assignRoleToUser(EMAIL_PORTAL_ADMIN, ROLE_PORTAL_ADMIN, user); 				
		api.assignRoleToUser(EMAIL_PORTAL_READONLY, ROLE_PORTAL_READONLY, user); 		
		api.assignRoleToUser(EMAIL_PORTAL_USER, ROLE_PORTAL_USER, user); 				
	}
	
	public void tearDown() {		
		userServiceTestHelper.tearDown();
		
		try {
			dataStoreTestHelper.tearDown();
		} catch (Exception e) {
			// data store test helper throws null pointer exception 
			// when used userservice test helper
			
			//swallow the exception for now as a temporary hack till the root cause
			//is identified
		}
	}
}
